package com.glsc.ngateway.common.api.platform.dto.gitee;

import com.glsc.ngateway.common.api.common.utils.CommonUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @author zhengyr
 * @date 2024/1/23-14:41
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("查询gitee任务参数")
public class SearchIssueParamDto implements Serializable {
    private static final String ISSUE_STATE = "issue_state";
    private static final String ISSUE_TYPE = "issue_type";
    private static final String ISSUE_ASSIGNEE = "issue_assignee";
    private static final String ISSUE_PROGRAM = "issue_program";

    @ApiModelProperty(value = "任务状态id多个 字典:gitee_issue_state 待办的、测试中、已结束等")
    private List<String> issueStateIdList;

    @ApiModelProperty(value = "任务类型id一个 字典:gitee_issue_type 任务、需求、缺陷")
    private String issueTypeId;
    @ApiModelProperty(value = "负责人id")
    private String assigneeId;

    @ApiModelProperty(value = "协作者id多个")
    private List<String> collaboratorIdList;
    @ApiModelProperty(value = "项目id多个 字典:gitee_issue_program")
    private List<String> issueProgramIdList;

    @ApiModelProperty(value = "关键词筛选")
    private String search;
    @ApiModelProperty(value = "状态")
    private String state;
    @ApiModelProperty(value = "优先级 多个,分割")
    private String priority;

    @ApiModelProperty(value = "计划开始时间")
    private String plan_started_at;
    @ApiModelProperty(value = "计划截止时间")
    private String deadline;
    @ApiModelProperty(value = "创建时间格式：(区间)yyyymmddTHH:MM:SS+08:00-yyyymmddTHH:MM:SS+08:00，（指定某日期）yyyymmddTHH:MM:SS+08:00，（小于指定日期）<yyyymmddTHH:MM:SS+08:00，（大于指定日期）>yyyymmddTHH:MM:SS+08:00")
    private String created_at;

    @ApiModelProperty(value = "pageNo")
    private Integer pageNo;
    @ApiModelProperty(value = "pageSize")
    private Integer pageSize;

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class IssueV2FilterDto {
        private String access_token;
        @ApiModelProperty(value = "任务状态id多个,分割 字典:gitee_issue_state 待办的、测试中、已结束等")
        private String issue_state_ids;
        @ApiModelProperty(value = "任务类型id一个 字典:gitee_issue_type 任务、需求、缺陷")
        private String issue_type_id;

        @ApiModelProperty(value = "负责人id")
        private String assignee_id;
        @ApiModelProperty(value = "协作者id多个,分割")
        private String collaborator_ids;

        @ApiModelProperty(value = "项目id多个,分割 字典:gitee_issue_program")
        private String program_id;
        private String state;
        private String priority;
        private String search;
        private String plan_started_at;
        private String deadline;
        private String created_at;
        @Builder.Default
        private Integer page = 1;
        @Builder.Default
        private Integer per_page = 100;
    }

    public IssueV2FilterDto buildIssuseV2FilterDto() {
        IssueV2FilterDto issueV2FilterDto = IssueV2FilterDto.builder()
                .issue_state_ids(CommonUtils.ListJoin2String(this.getIssueStateIdList()))
                .issue_type_id(this.getIssueTypeId())
                .assignee_id(this.getAssigneeId())
                .collaborator_ids(CommonUtils.ListJoin2String(this.getCollaboratorIdList()))
                .program_id(CommonUtils.ListJoin2String(this.getIssueProgramIdList()))
                .build();
        BeanUtils.copyProperties(this, issueV2FilterDto);
        return issueV2FilterDto;
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class FilterCondition {
        @Builder.Default
        private String comparator = "contains";
        private String property;
        private List<?> value;
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class IssuseFilterDto {
        private List<FilterCondition> filter_conditions;
        private String access_token;
        private Integer page;
        private Integer per_page;
        @Builder.Default
        private Integer offset = 0;
        @Builder.Default
        private String result_version = "v2";
        @Builder.Default
        private String mode = "tree";
        private String search;
    }

//    public IssuseFilterDto buildIssuseFilterDto(String accessToken) {
//        List<FilterCondition> filter_conditions = new ArrayList<>();
//        if (CollectionUtil.isNotEmpty(this.issueStateIdList)) {
//            filter_conditions.add(FilterCondition.builder().
//                    property(ISSUE_STATE).
//                    value(this.issueStateIdList).build());
//        }
//        if (CollectionUtil.isNotEmpty(this.issueTypeIdList)) {
//            filter_conditions.add(FilterCondition.builder().
//                    property(ISSUE_TYPE).
//                    value(this.issueTypeIdList).build());
//        }
//        if (CollectionUtil.isNotEmpty(this.issueAssigneeIdList)) {
//            filter_conditions.add(FilterCondition.builder().
//                    property(ISSUE_ASSIGNEE).
//                    value(this.issueAssigneeIdList).build());
//        }
//        if (CollectionUtil.isNotEmpty(this.issueProgramIdList)) {
//            filter_conditions.add(FilterCondition.builder().
//                    property(ISSUE_PROGRAM).
//                    value(this.issueProgramIdList).build());
//        }
//        return IssuseFilterDto.builder().
//                access_token(accessToken).
//                filter_conditions(filter_conditions).
//                page(this.pageNo).
//                per_page(this.pageSize).
//                search(this.search).build();
//    }

}
